package com.atguigu.statemachine.service;

import com.atguigu.statemachine.config.OrderEvents;
import com.atguigu.statemachine.config.OrderStatus;
import com.atguigu.statemachine.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;
import org.springframework.stereotype.Component;

/**
 * @author Jungle
 * @create 2025-05-22 8:44
 * @description
 */

@Slf4j
@Component
@WithStateMachine(name = "orderStateMachine")
public class OrderStateListener {

    /**
     * 方法触发条件：订单状态  init->payed
     */
    @OnTransition(source = "INIT", target = "PAYED")
    public Boolean pay(Message<OrderEvents> message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setStatus(OrderStatus.PAYED);
        log.info("订单表保存数据");
        log.info("发送站内消息");
        log.info("回调支付接口");
        log.info("同步至数据仓库");
        return true;
    }

    /**
     * 方法触发条件：订单状态  payed->shipped
     */
    @OnTransition(source = "PAYED", target = "SHIPPED")
    public Boolean shopping(Message<OrderEvents> message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setStatus(OrderStatus.SHIPPED);
        log.info("==>{}", "创建物流订单");
        log.info("==>{}", "更新物流订单");
        log.info("==>{}", "同步至数据仓库");
        return true;
    }

    /**
     * 方法触发条件：订单状态  shipped->received
     */
    @OnTransition(source = "SHIPPED", target = "RECEIVED")
    public Boolean receive(Message<OrderEvents> message) {
        Order order = (Order) message.getHeaders().get("order");
        order.setStatus(OrderStatus.PAYED);
        log.info("==>{}", "更新订单数据");
        log.info("==>{}", "同步至数据仓库");
        return true;
    }




}
